﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpeedTest
{
	public partial class PanelSuite : UserControl
	{
		private Data data = null;
		private string itemID = String.Empty;
		private DataTable suiteTable = null;
		private EditMode mode = EditMode.EditMode;
		private string planID = String.Empty;
		private string parentPath = String.Empty;

		public PanelSuite(Data data, EditMode mode, string planID, string parentPath)
		{
			InitializeComponent();
			this.mode = mode;
			this.data = data;
			this.planID = planID;
			this.parentPath = parentPath;
			EnableInput(true);
		}

		public PanelSuite(Data data, string itemID, bool enableInput)
		{
			InitializeComponent();
			this.data = data;
			this.itemID = itemID;
			LoadData();
			EnableInput(false);
			
		}

		public delegate void OnSave(string id, NodeType nodeType);
		public event OnSave Save;
		public delegate void OnCancel();
		public event OnCancel Cancel;

		private bool LoadData()
		{
			if (data != null)
			{
				suiteTable = data.GetSuiteByID(itemID);
			}
			else { return false; }

			if (suiteTable != null)
			{
				txtTitle.Text = suiteTable.Rows[0]["Description"].ToString();
				txtSetup.Text = suiteTable.Rows[0]["Steps"].ToString();
			}
			else { return false; }

			return true;
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			EnableInput(true);
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
            if (!ValidateInput()) { return; }

			if (mode == EditMode.EditMode)
			{
				suiteTable.Rows[0]["Description"] = txtTitle.Text;
				suiteTable.Rows[0]["Steps"] = txtSetup.Text;

				data.UpdateSuiteByID(suiteTable, itemID);
			}
			else
			{
				//[ParentPlanID], [NodeType], [Path], [Description], [CreatorID]
				suiteTable = new DataTable();
				suiteTable.Columns.Add("ParentPlanID");
				suiteTable.Columns.Add("NodeType");
				suiteTable.Columns.Add("Path");
				suiteTable.Columns.Add("Description");
				suiteTable.Columns.Add("CreatorID");
				suiteTable.Columns.Add("Steps");

				string[] record = new string[6];
				record[0] = planID;
				record[1] = "1";	//NodeType.SuiteNode
				record[2] = data.GetCaseSuitePath(planID, parentPath);
				record[3] = txtTitle.Text;
				record[4] = data.User.UserID;
				record[5] = txtSetup.Text;

				suiteTable.Rows.Add(record);

				itemID = data.InsertSuite(suiteTable).ToString();
			}

			EnableInput(false);
			Save.Invoke(itemID, NodeType.SuiteNode);
		}

        private bool ValidateInput()
        {
            string requiredFields = String.Empty;

            if (String.IsNullOrEmpty(txtTitle.Text)) { requiredFields += "Title\n"; }

            if (!String.IsNullOrEmpty(requiredFields))
            {
                MessageBox.Show("The following required fields are empty\n" + requiredFields, "Error");
                return false;
            }

            return true;
        }

		private void btnCancel_Click(object sender, EventArgs e)
		{
			if (mode == EditMode.EditMode)
			{
				LoadData();
				EnableInput(false);
			}
			else
			{
				Cancel.Invoke();
			}
		}

		private void EnableInput(bool enable)
		{
			txtTitle.Enabled = enable;
			txtSetup.Enabled = enable;

			if (IsPermitted(btnEdit, data.User.Plans)) { btnEdit.Enabled = !enable; }
			else { btnEdit.Enabled = false; }
			btnSave.Enabled = enable;
			btnCancel.Enabled = enable;
		}

		private bool IsPermitted(Button btn, Permissions perm)
		{
			if (btn == btnEdit) { return perm.Edit; }

			return false;
		}
	}
}
